package fr.m2i.cinescopeandroid;

import fr.m2i.cinescopeandroid.R;
import fr.m2i.cinescopeandroid.R.id;
import fr.m2i.cinescopeandroid.R.layout;
import fr.m2i.cinescopeandroid.R.raw;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.media.MediaPlayer;
import android.net.Uri;

//--------------------------------------------------------------------
public class MonSon extends Activity implements OnClickListener
// -------------------------------------------------------------------
{
	/*
	 * resources : bris_verre.mp3
	 * data : sword.mp3
	 * SD : coucou_horloge.mp3
	 * Web : coucou_horloge.mp3
	 */

	private RadioButton radioRessource;
	private RadioButton radioData;
	private RadioButton radioSD;
	private RadioButton radioWeb;

	private Button buttonDemarrerSon;
	private Button buttonArreterSon;

	private TextView textViewMessage;

	private MediaPlayer iMP;

	private Context contexte;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mon_son);

		radioRessource = (RadioButton) findViewById(R.id.radioRessource);
		radioData = (RadioButton) findViewById(R.id.radioData);
		radioSD = (RadioButton) findViewById(R.id.radioSD);
		radioWeb = (RadioButton) findViewById(R.id.radioWeb);

		buttonDemarrerSon = (Button) findViewById(R.id.buttonDemarrerSon);
		buttonArreterSon = (Button) findViewById(R.id.buttonArreterSon);

		textViewMessage = (TextView) findViewById(R.id.textViewMessage);

		buttonDemarrerSon.setOnClickListener(this);
		buttonArreterSon.setOnClickListener(this);

		contexte = this;

	} // Fin onCreate

	@Override
	public void onClick(View v) {

		if (v == buttonDemarrerSon) {
			String lsChemin = "";
			textViewMessage.setText(">");

			/*
			 * Fichier stocke dans /res/raw/
			 */
			if (radioRessource.isChecked()) {
				lsChemin = "android.resource://" + getPackageName() + "/" + R.raw.bris_verre;
			}

			/*
			 * Fichier stocke dans /data/data/.../files/
			 */
			if (radioData.isChecked()) {
				lsChemin = "file://" + contexte.getFilesDir().getPath() + "/sons/art_mengo.mp3";
			}

			/*
			 * SUR LA SD
			 */
			if (radioSD.isChecked()) {
				lsChemin = Environment.getExternalStorageDirectory().getPath() + "/sons/coucou_horloge.mp3";
			}

			/*
			 * Fichier sur le WEB
			 */
			if (radioWeb.isChecked()) {
				lsChemin = "http://10.3.50.124/sons/coucou_horloge.mp3";
			}

			// --- Dans tous les cas
			// En mode create
			iMP = MediaPlayer.create(contexte, Uri.parse(lsChemin));
			iMP.start();
		} // Fin if buttonDemarrerSon

		if (v == buttonArreterSon) {
			iMP.stop();
			iMP.release();
			textViewMessage.setText("||");
		} // Fin if buttonArreterSon

	} /// onClick

	@Override
	protected void onPause() {
		super.onPause();
		// --- Liberation des ressources si necessaire
		if (iMP != null) {
			iMP.release();
			iMP = null;
		}
	} /// onPause
} /// classe